package cc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.GetUserList;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.common.utils.RestTemplateUtil;
import com.bootdo.common.wx.WeiXinUtil;
import com.bootdo.system.domain.Articles;
import com.bootdo.system.domain.Media_id;
import com.bootdo.system.domain.News;
import com.bootdo.system.domain.SendPojo;
import com.bootdo.system.domain.TbScgl;


public class csfsd {
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		  String accessToken =WeiXinUtil.getOauth2AccessToken2("wxcb74c2a7fb1fd161", "3ba771e951e1e0e917b7dcd777a25e06");
		RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
	    Articles articles = createArticles();
	    if (accessToken != null) {
	        String url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token="+accessToken;
	        //将菜单对象转换成JSON字符串
	        String jsonNews = JSONObject.toJSONString(articles);
	        //发起POST请求创建菜单
	        String jsonObject = restTemplate.postForObject(url, jsonNews,String.class);
	        System.out.println(jsonObject);
	    }
		
	}
	
	private static Articles createArticles(){
	    Articles articles = new Articles();
	    List<News> dataList = new ArrayList<News>();
	    News  news1 = new News();
	    news1.setTitle("ceshi");
	    news1.setThumb_media_id("Wr8SVx7Dgzgll4I2-xlgj-MfBPee5XYMZaJkE7CMhGc");
	    news1.setAuthor("");
	    news1.setShow_cover_pic(1);//显示封面
	    news1.setContent("<mp-miniprogram data-miniprogram-appid=\"wx798d61260ba95d82\" data-miniprogram-path=\"pages/index/index\" data-miniprogram-title=\"小程序示例\" data-miniprogram-imageurl=\"https://chengxh.cn1.utools.club/upLoad/ccccc.jpg\"></mp-miniprogram>");
//	    news1.setContent("aa");
	    news1.setContent_source_url("https://www.baidu.com/");  //图文消息的原文地址，即点击“阅读原文”后的URL
	    dataList.add(news1);
	    articles.setArticles(dataList);
	    return articles;

	}
	
	public String sendMsg(String token,String id) throws ClientProtocolException, IOException {
		//先获取用户openid列表
		String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token="+token;
		List<String> userList =new ArrayList<String>();
		userList.add("oLIwp5rKXaKKnrYRrdn7OtsevNOA");
		String openid = userList.toString();
		System.out.println(openid);
		Media_id m = new Media_id();
		m.setMedia_id(id);
		JSONObject json1 = (JSONObject) JSONObject.toJSON(m);
		String str1 = json1.toString();
		System.out.println(str1);
		//封装请求参数
		SendPojo sp = new SendPojo();
		sp.setTouser(userList);
		sp.setMpnews(str1);
		sp.setMsgtype("mpnews");
		sp.setSend_ignore_reprint(0);
		JSONObject json = (JSONObject) JSONObject.toJSON(sp);
		String str = json.toString();
		str= str.replace("\\", "");  
		str= str.replace("\"{", "{");
		str= str.replace("}\"", "}");
		System.out.println(str);
		return HttpUtil.httpPost(url, null, str);
	}

}
